package models;

import java.util.*;
import javax.persistence.*;

import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;
import com.avaje.ebean.*;

@Entity
public class Qcm extends Model {
	@Id
	private Long idQcm;
	private User user;
	private String libelle;

	private Date dateCreation;
	private Date dateOuverture;
	private Date dateCloture;

	private String cleAuthentification;
	private List<Question> listeQuestions = new ArrayList<Question>();
	private List<QcmNiveauquestion> listeNiveauQuestions = new ArrayList<QcmNiveauquestion>();

	public static Finder<Long, Qcm> find = new Finder<Long, Qcm>(Long.class,
			Qcm.class);

	/**
	 * La méthode permet de retrouver un QCM à partir de son ID
	 * 
	 * @param id
	 * @return Qcm
	 */
	public static Qcm findById(Long id) {
		return Ebean.find(Qcm.class, id);
	}

	/**
	 * La méthode permet de renvoyer la liste de tous les QCM
	 */
	public static List<Qcm> findAll() {
		return find.all();
	}

	public String getStatut() {
		Date now = new Date();
		String statut;
		if (now.compareTo(dateCloture) > 0) {
			statut = "Clos";
		} else if (now.compareTo(dateOuverture) < 0) {
			statut = "A venir";
		} else {
			statut = "Ouvert";
		}
		return statut;
	}

	public QcmNiveauquestion findQcmNiveauQuestion(NiveauQuestion nq) {
		Iterator<QcmNiveauquestion> itnq = listeNiveauQuestions.iterator();
		while (itnq.hasNext()) {
			QcmNiveauquestion qnq = itnq.next();
			if (qnq.getNiveauQuestion().equals(nq))
				return qnq;
		}
		return null;
	}

	/*
	 * GETTER ET SETTER
	 */
	public Long getIdQcm() {
		return idQcm;
	}

	public void setIdQcm(Long idQcm) {
		this.idQcm = idQcm;
	}

	@ManyToOne
	public User getUser() {
		return user;
	}

	public void setUser(User User) {
		this.user = user;
	}

	public Date getDateOuverture() {
		return dateOuverture;
	}

	public void setDateOuverture(Date dateOuverture) {
		this.dateOuverture = dateOuverture;
	}

	public Date getDateCloture() {
		return dateCloture;
	}

	public void setDateCloture(Date dateCloture) {
		this.dateCloture = dateCloture;
	}

	public String getCleAuthentification() {
		return cleAuthentification;
	}

	public void setCleAuthentification(String cleAuthentification) {
		this.cleAuthentification = cleAuthentification;
	}

	@ManyToMany
	public List<Question> getListeQuestions() {
		return listeQuestions;
	}

	public void setListeQuestions(List<Question> listeQuestions) {
		this.listeQuestions = listeQuestions;
	}

	public String getLibelle() {
		return libelle;
	}

	public void setLibelle(String libelle) {
		this.libelle = libelle;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	@OneToMany(mappedBy = "qcm")
	public List<QcmNiveauquestion> getListeNiveauQuestions() {
		return listeNiveauQuestions;
	}

	public void setListeNiveauQuestions(
			List<QcmNiveauquestion> listeNiveauQuestions) {
		this.listeNiveauQuestions = listeNiveauQuestions;
	}

}
